﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Electric;
using YArchitech.LIB;

namespace HYRElectric.PowerSystem
{
	public partial class DlgSystemLoop : System.Windows.Forms.Form
	{
		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		public DlgSystemLoop(ExternalCommandData cmdData, FamilyInstance panel)
		{
			this.m_uiDoc = cmdData.Application.ActiveUIDocument;
			this.m_doc = this.m_uiDoc.Document;
			this.m_panel = panel;
			this.m_loopInfoManager = new LoopInfoManager(this.m_panel, this.m_doc);
			this.m_dicloopInfo = new Dictionary<string, LoopInfoItem>();
			this.m_loopInfoManager.GetLoopInfo(ref this.m_dicloopInfo);
			this.InitializeComponent();
			this.txtSwitchBoxNum.Text = this.m_loopInfoManager.GetPanelNumber();
			this.txtPrefix.Text = this.m_loopInfoManager.GetPanelPrefix();
			this.txtSeparator.Text = this.m_loopInfoManager.GetPanelSeparator();
			this.RefreshLoopInfoLvw();
			if (this.lvwLoopInfo.Items.Count > 0)
			{
				this.lvwLoopInfo.Items[0].Selected = true;
			}
			this.m_materialStatistic = new PanelMaterialStatistic();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			this.m_nOkType = 0u;
			base.DialogResult = DialogResult.OK;
			this.m_winPt = base.Location;
		}

		public void AddNewLoop(IList<Reference> lstEquips)
		{
			this.m_loopInfoManager.AddNewLoop(lstEquips);
			this.m_dicloopInfo.Clear();
			this.m_loopInfoManager.GetLoopInfo(ref this.m_dicloopInfo);
			this.RefreshLoopInfoLvw();
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			ListView.SelectedListViewItemCollection selectedItems = this.lvwLoopInfo.SelectedItems;
			if (selectedItems.Count <= 0)
			{
				YJKMessageBox.Show("请选中一条回路信息");
				base.DialogResult = DialogResult.Retry;
				return;
			}
			this.m_strKey = selectedItems[0].SubItems[0].Text;
			LoopInfoItem loopInfoItem = this.m_dicloopInfo[this.m_strKey];
			this.m_loopInfoManager.DeleteLoop(loopInfoItem.loopId);
			this.m_dicloopInfo.Remove(this.m_strKey);
			this.RefreshLoopInfoLvw();
		}

		private void btnAddEquip_Click(object sender, EventArgs e)
		{
			ListView.SelectedListViewItemCollection selectedItems = this.lvwLoopInfo.SelectedItems;
			if (selectedItems.Count <= 0)
			{
				YJKMessageBox.Show("请选中一条回路信息");
				base.DialogResult = DialogResult.Retry;
				return;
			}
			this.m_strKey = selectedItems[0].SubItems[0].Text;
			this.m_nOkType = 1u;
			base.DialogResult = DialogResult.OK;
			this.m_winPt = base.Location;
		}

		public void AddEquipsToLoop(IList<Reference> lstEquips)
		{
			LoopInfoItem loopInfoItem = this.m_dicloopInfo[this.m_strKey];
			this.m_loopInfoManager.AddEquipsToLoop(loopInfoItem.loopId, lstEquips);
			this.m_dicloopInfo.Clear();
			this.m_loopInfoManager.GetLoopInfo(ref this.m_dicloopInfo);
			this.RefreshLoopInfoLvw();
		}

		private void btnApplyPre_Click(object sender, EventArgs e)
		{
			string text = this.txtPrefix.Text;
			this.m_loopInfoManager.ModifyLoopNumber(text);
			this.m_dicloopInfo.Clear();
			this.m_loopInfoManager.GetLoopInfo(ref this.m_dicloopInfo);
			this.RefreshLoopInfoLvw();
		}

		private void btnApplyNum_Click(object sender, EventArgs e)
		{
			string text = this.txtSwitchBoxNum.Text;
			this.m_loopInfoManager.ModifySwitchBoxNum(text);
		}

		private void btnApplySpe_Click(object sender, EventArgs e)
		{
			string text = this.txtSeparator.Text;
			this.m_loopInfoManager.ModifySeparator(text);
			this.m_dicloopInfo.Clear();
			this.m_loopInfoManager.GetLoopInfo(ref this.m_dicloopInfo);
			this.RefreshLoopInfoLvw();
		}

		private void btnWin_Click(object sender, EventArgs e)
		{
			if (this.m_bShow)
			{
				int width = base.Width - this.lvwMeterialInfo.Width - 15;
				int height = base.Height;
				base.Size = new Size(width, height);
				this.MaximumSize = new Size(width, height);
				this.MinimumSize = new Size(width, height);
				this.m_bShow = false;
				this.btnWin.Image = YArchitech.Electric.Resources.GetImage("Right");
				return;
			}
			int width2 = base.Width + this.lvwMeterialInfo.Width + 15;
			int height2 = base.Height;
			base.Size = new Size(width2, height2);
			this.MaximumSize = new Size(width2, height2);
			this.MinimumSize = new Size(width2, height2);
			this.m_bShow = true;
			this.btnWin.Image = YArchitech.Electric.Resources.GetImage("Left");
		}

		private void btnMeterialInfo_Click(object sender, EventArgs e)
		{
			if (this.m_dicloopInfo.Count < 1)
			{
				YJKMessageBox.Show("系统回路中没有回路信息！");
				base.DialogResult = DialogResult.Retry;
				return;
			}
			if (this.m_lstMaterial.Count <= 0)
			{
				YJKMessageBox.Show("请选中一条回路信息");
				base.DialogResult = DialogResult.Retry;
				return;
			}
			base.DialogResult = DialogResult.Yes;
			this.m_winPt = base.Location;
		}

		private void RefreshLoopInfoLvw()
		{
			this.lvwLoopInfo.Items.Clear();
			if (this.m_dicloopInfo.Count <= 0)
			{
				this.lvwMeterialInfo.Items.Clear();
				return;
			}
			foreach (KeyValuePair<string, LoopInfoItem> keyValuePair in this.m_dicloopInfo)
			{
				LoopInfoItem value = keyValuePair.Value;
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = keyValuePair.Key;
				listViewItem.SubItems.Add(value.dPower.ToString("0.0"));
				this.lvwLoopInfo.Items.Add(listViewItem);
			}
			if (this.lvwLoopInfo.SelectedItems.Count <= 0)
			{
				this.lvwLoopInfo.Items[0].Selected = true;
			}
		}

		private void DlgSystemLoop_Load(object sender, EventArgs e)
		{
			this.btnAdd.Image = YArchitech.Electric.Resources.GetImage("AddLoop");
			this.btnDelete.Image = YArchitech.Electric.Resources.GetImage("DelLoop");
			this.btnAddEquip.Image = YArchitech.Electric.Resources.GetImage("AddToLoop");
			this.btnWin.Image = YArchitech.Electric.Resources.GetImage("Left");
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.btnAdd, "添加回路");
			toolTip.SetToolTip(this.btnDelete, "删除回路");
			toolTip.SetToolTip(this.btnAddEquip, "追加设备");
			this.MaximumSize = new Size(base.Width, base.Height);
			this.MinimumSize = new Size(base.Width, base.Height);
			//base.Icon = Resource.YArchitechIcon;
            if (this.m_winPt != System.Drawing.Point.Empty)
			{
				base.Location = this.m_winPt;
			}
		}

		private void lvwLoopInfo_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.lvwMeterialInfo.Items.Clear();
			ListView.SelectedListViewItemCollection selectedItems = this.lvwLoopInfo.SelectedItems;
			if (selectedItems.Count <= 0)
			{
				return;
			}
			this.m_strKey = selectedItems[0].SubItems[0].Text;
			LoopInfoItem loopInfoItem = this.m_dicloopInfo[this.m_strKey];
			List<ElementId> list = new List<ElementId>();
			List<Element> list2 = new List<Element>();
			foreach (ElementId elementId in loopInfoItem.lstItemIds)
			{
				list.Add(elementId);
				list2.Add(this.m_doc.GetElement(elementId));
			}
			foreach (ElementId elementId2 in loopInfoItem.lstWireId)
			{
				list.Add(elementId2);
				list2.Add(this.m_doc.GetElement(elementId2));
			}
			if (this.m_lstMaterial != null)
			{
				this.m_lstMaterial.Clear();
			}
			this.m_lstMaterial = this.m_materialStatistic.GetMaterialInfo(this.m_doc, list);
			foreach (MaterialInfo materialInfo in this.m_lstMaterial)
			{
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = materialInfo.strName;
				listViewItem.SubItems.Add(materialInfo.strSpec);
				if (materialInfo.strUnit.CompareTo("米") == 0)
				{
					listViewItem.SubItems.Add(materialInfo.dLength.ToString("0.0"));
				}
				else
				{
					listViewItem.SubItems.Add(materialInfo.nCount.ToString());
				}
				listViewItem.SubItems.Add(materialInfo.strUnit);
				this.lvwMeterialInfo.Items.Add(listViewItem);
			}
			RevitVersionFuncs.SetSelection(this.m_uiDoc.Selection, list2);
			this.m_uiDoc.RefreshActiveView();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		public LoopInfoManager m_loopInfoManager;

		private FamilyInstance m_panel;

		private Document m_doc;

		private UIDocument m_uiDoc;

		public Dictionary<string, LoopInfoItem> m_dicloopInfo;

		public List<MaterialInfo> m_lstMaterial = new List<MaterialInfo>();

		public string m_strKey;

		public uint m_nOkType;

		private bool m_bShow = true;

        private System.Drawing.Point m_winPt = System.Drawing.Point.Empty;

		private PanelMaterialStatistic m_materialStatistic;
	}
}
